package com.dht.xiao.jianye.utils;

import android.content.Context;

import com.dht.xiao.jianye.bean.DaoMaster;
import com.dht.xiao.jianye.bean.DaoSession;
import com.dht.xiao.jianye.bean.OrderDaoBean;
import com.dht.xiao.jianye.bean.OrderDaoBeanDao;

import java.util.List;

public class DBManager {
    private Context mContext;
    private DaoMaster mDaoMaster;
    private DaoSession mDaoSession;
    private OrderDaoBeanDao orderDaoBeanDao;

    private DBManager(Context context) {
        mContext = context;
    }

    private static volatile DBManager instance = null;
    public static DBManager getInstance(Context context){
        if (instance==null){
            synchronized (DBManager.class){
                if (instance==null){
                    instance = new DBManager(context);
                }
            }
        }
        return instance;
    }

    public void init() {
        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(mContext, "user.db");
        mDaoMaster = new DaoMaster(helper.getWritableDb());
        mDaoSession = mDaoMaster.newSession();
        orderDaoBeanDao = mDaoSession.getOrderDaoBeanDao();
    }

    public void insertTopicMo(OrderDaoBean user) {
        orderDaoBeanDao.insertOrReplace(user);
    }

    public void insertTopicMo(List<OrderDaoBean> user) {
        orderDaoBeanDao.insertOrReplaceInTx(user);
    }
    //单个删除
    public void deleteTopicMo(OrderDaoBean user) {
        orderDaoBeanDao.delete(user);
    }
    //删除所有
    public void deleteTopicMo() {
        orderDaoBeanDao.deleteAll();
    }
    //更新
    public void updateTopicMo(OrderDaoBean user) {
        orderDaoBeanDao.update(user);
    }
    //查询所有
    public List<OrderDaoBean> queryAllTopicMo() {
        return orderDaoBeanDao.queryBuilder().build().list();
    }
    //where查询
    public List<OrderDaoBean> query(int Report) {
        return orderDaoBeanDao.queryBuilder().where(OrderDaoBeanDao.Properties.Report.eq(Report)).list();
    }
    //between
    public List<OrderDaoBean> queryarrang(int a,int b) {
        return orderDaoBeanDao.queryBuilder().where(OrderDaoBeanDao.Properties.Id.between(a,b)).list();
    }


}
